using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;

namespace Emlyn.Applications.Scratch.Graphics.Chapter2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        PresentParameters _presentParameters = null;
        Device _device = null;
        Surface _backbuffer = null;
        Surface _loadedimage = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            InitDirectX();

            _backbuffer = GetBackBuffer();

            _loadedimage = LoadSurface(@"C:\Program Files\Microsoft Visual Studio 8\VC\ce\atlmfc\include\res\3dcheck.bmp");
        }

        private Surface LoadSurface(string aFilename)
        {
            Surface retval = null;

            retval = new Surface(_device, new Bitmap(aFilename), Pool.Managed);

            return retval;
        }

        public void InitDirectX()
        {
            _presentParameters = new PresentParameters();

            //_device = new Device();

            AdapterInformation ladapterInformation =
                Manager.Adapters[0];

            DeviceType ldeviceType = GetSupportedDeviceType();

            _presentParameters.SwapEffect = SwapEffect.Discard;
            _presentParameters.Windowed = true;
            _presentParameters.BackBufferWidth = 640;
            _presentParameters.BackBufferHeight = 480;
            _presentParameters.BackBufferCount = 1;
            _presentParameters.BackBufferFormat = 
                ladapterInformation.CurrentDisplayMode.Format;

            _device = new Device(
                0,
                ldeviceType,
                this.Handle,
                CreateFlags.SoftwareVertexProcessing,
                _presentParameters
                );

            this.Text = ldeviceType.ToString();
        }

        Surface GetBackBuffer()
        {
            return _device.GetBackBuffer(0, 0, BackBufferType.Mono);
        }



        private DeviceType GetSupportedDeviceType()
        {
            DeviceType retval = DeviceType.Hardware;

            try
            {
                Caps lcaps =
                Manager.GetDeviceCaps(0, retval);
            }
            catch
            {
                try
                {
                    retval = DeviceType.Software;
                    Caps lcaps =
                    Manager.GetDeviceCaps(0, retval);
                }
                catch
                {
                    try
                    {
                        retval = DeviceType.Reference;
                        Caps lcaps =
                        Manager.GetDeviceCaps(0, retval);
                    }
                    catch
                    {
                        throw new ApplicationException("Can't determine device type.");
                    }
                }
            }

            return retval;
        }

        int i = 0;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // _device.Clear(ClearFlags.Target, Color.Red, 0, 0);

            DoPaint();
        }

        private void DoPaint()
        {
            Rectangle _rectimage = new Rectangle(
                0, 0, _loadedimage.Description.Width - 1, _loadedimage.Description.Height - 1);
            Rectangle _rectbb = new Rectangle(
                0, 0, _backbuffer.Description.Width - 1, _backbuffer.Description.Height - 1);
            _device.StretchRectangle(
                _loadedimage, _rectimage, _backbuffer, _rectbb, TextureFilter.None
            );

            _device.Present();

            this.Text = i++.ToString();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        private void Form1_ResizeBegin(object sender, EventArgs e)
        {
        }

        private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}